<?php
//error_reporting(E_ALL);

$c = mysql_connect('localhost','root','?');

//database name
$database = 'ppomo';
//table name
$table = 'realty';
// validation :  witch fields must have unique values
$unique = array();
// validation :  witch fields required
$required = array('today','city','street','house','flats','floors','floor','square','price');
// validation :  witch fields must have email validation
$email = array();
// validation :  witch fields must have date validation
$date = array('today');
// validation :  witch fields must have time validation
$time = array();
// validation :  witch fields must be more then 0
$morethenzero = array('flats','floors','floor','square');

mysql_select_db($database);
$res = mysql_query('Describe '.$table);  
$fields = array();
While($row = mysql_fetch_assoc($res)) {
    //echo '<b><h3>'.$row['Field'].' '.$row['Type'].' ['.substr($row['Type'],0,3).']</h3></b><br>';
    $fields[$row['Field']] = $row['Type'];
}
$key = array_shift($fields);
?><pre>

class <?php echo ucfirst($table);?>Controller extends Zend_Controller_Action 
{
    function init()
    {
        $this->initView();
        Zend_Loader::loadClass('AuxDbTable');
        Zend_Loader::loadClass('User');
        Zend_Loader::loadClass('<?php echo ucfirst($table);?>');
        Zend_Loader::loadClass('Authgroup');
        Zend_Loader::loadClass('Mainmenu');
        Zend_Loader::loadClass('Rights');
        Zend_Loader::loadClass('Authority'); 
        $this->view->baseUrl = $this->_request->getBaseUrl();
        $this->view->user = Zend_Auth::getInstance()->getIdentity();
    }
    
    function preDispatch()
    {
        $auth = Zend_Auth::getInstance();
        if (!$auth->hasIdentity()) {
            $this->_redirect('auth/login');
        }
        $menu = new Mainmenu();
        $this->view->main_menu = $menu->getMenu($this->view->user->id);
    }

    function indexAction()
    {
        $Rights = new Rights();
        if(!$Rights->CanDo($this->view->user->id,'<?php echo $table;?>','index')) $this->_redirect(DEFAUL_URL);
        
        $this->view->title = "<?php echo ucfirst($table);?>s";
        $this->view->content_title = "<?php echo ucfirst($table);?>s";
        $<?php echo $table;?> = new <?php echo ucfirst($table);?>();
        $this->view->items = $<?php echo $table;?>->fetchAll('deleted!=1');
        
        $this->view->can_add = $Rights->CanDo($this->view->user->id,'<?php echo $table;?>','add');
        $this->view->can_edit = $Rights->CanDo($this->view->user->id,'<?php echo $table;?>','edit');
        $this->view->can_delete = $Rights->CanDo($this->view->user->id,'<?php echo $table;?>','delete');
        
        $this->render();
    }
    
    function validate($id,$params)
    {
        $valid = true;
        $obj = new <?php echo ucfirst($table);?>();
        
        <?php foreach($required as $req) { ?>if(strlen($params['<?php echo $req;?>'])==0)
        {
            $this->view->message .= 'Please, enter <?php echo ucfirst($req);?>.&lt;br>';
            $valid = false;
        }    
        <?php  }
        foreach($unique as $u) { ?>if($valid)
        {
            if($obj->fetchRow('id <> '.$id.' and <?php echo $u;?>="'.$params['<?php echo $u;?>'].'"'))
            {
                $this->view->message .= '<?php echo ucfirst($table);?> : <?php echo ucfirst($u);?> '.$params['<?php echo $u;?>'].' already exist.&lt;br>';
                $valid = false;
            }
        }<?php  }
        
        
        foreach($morethenzero as $u) { ?>
        if(!$params['<?php echo $u;?>'])
        {
            $this->view->message .= '<?php echo ucfirst($u);?> must ne more then zero.&lt;br>';
            $valid = false;
        }
        <?php  }?>
        
        
        //email
        $email_pattern = '/^[A-Z0-9\._\-]+@[A-Z0-9\.\-]+\.[A-Z]{2,6}$/i';
        
        <?php foreach($email as $e) { ?>if(!preg_match($email_pattern,$params['<?php echo $e;?>']))
        {
            $this->view->message .= 'Invalid Email '.$params['<?php echo $e;?>'].'.&lt;br>';
            $valid = false;
        } 
        <?php }?>//date 
        $date_pattern = '/^[0-9]{4}\-(?:0[1-9]|1[0-2])\-(?:[0-2]?[0-9]|3[0-1])$/';
        <?php foreach($date as $d) { ?>if(!preg_match($date_pattern,$params['<?php echo $d;?>']))
        {
            $this->view->message .= 'Invalid <?php echo $d;?> '.$params['<?php echo $d;?>'].'. Format yyyy-mm-dd.&lt;br>';
            $valid = false;
        } 
        <?php }?>//time
        $time_pattern = '^(([0-9])|([0-1][0-9])|([2][0-3])):(([0-9])|([0-5][0-9]))$';
        <?php foreach($time as $t) { ?>if(!preg_match($time_pattern,$params['<?php echo $t;?>']))
        {
            $this->view->message .= 'Invalid <?php echo $t;?> '.$params['<?php echo $t;?>'].'. Format hh:mm.&lt;br>';
            $valid = false;
        } 
        <?php }?>
        
        return $valid;
    }
    function addAction()
    {
        $Rights = new Rights();
        if(!$Rights->CanDo($this->view->user->id,'<?php echo $table;?>','add')) $this->_redirect(DEFAUL_URL);
        
        $this->view->content_title = $this->view->title = "Add New <?php echo ucfirst($table);?>";
        $params = array();

        if (strtolower($_SERVER['REQUEST_METHOD']) == 'post') {
            Zend_Loader::loadClass('Zend_Filter_StripTags');
            $filter = new Zend_Filter_StripTags();
            
            <?php foreach($fields as $f => $t) { ?>$params['<?php echo $f;?>'] = trim($filter->filter($this->_request->getPost('<?php echo $f;?>')));
            <?php } ?>
        
            $this->view->message = '';
            if($this->validate(0,$params)) {
                $obj = new <?php echo ucfirst($table);?>();
                $new_id = $obj->insert($params);
                
                $this->_redirect('/<?php echo $table;?>');
                return;
            }
        } 
        
        // set up an "empty" <?php echo $table;?>
        
        $this->view->item = new stdClass();
        $this->view->item->id = null;
        <?php foreach($fields as $f => $t) { ?>$this->view->item-><?php echo $f;?> = isset($params['<?php echo $f;?>'])?$params['<?php echo $f;?>']:'';
        <?php } ?>                
                    
        // additional view fields required by form
        $this->view->action = 'add';
        $this->render();
    }
    
    function editAction()
    {
        $Rights = new Rights();
        if(!$Rights->CanDo($this->view->user->id,'<?php echo $table;?>','edit')) $this->_redirect(DEFAUL_URL);
            
        $this->view->content_title = $this->view->title = "Edit <?php echo ucfirst($table);?>";
        $obj = new <?php echo ucfirst($table);?>();
        
        if (strtolower($_SERVER['REQUEST_METHOD']) == 'post') {
            Zend_Loader::loadClass('Zend_Filter_StripTags');
            $filter = new Zend_Filter_StripTags();
            
            $id = (int)$this->_request->getPost('id');
            $params = array();
            <?php foreach($fields as $f => $t) { ?>$params['<?php echo $f;?>'] = trim($filter->filter($this->_request->getPost('<?php echo $f;?>')));
            <?php } ?>
            
            if ($id !== false) {
                $this->view->message = '';
                if($this->validate($id,$params)) {
                    $where = 'id = ' . $id;
                    $obj->update($params, $where);
                
                    $this->_redirect('/<?php echo $table;?>');
                    return;
                } else {
                    $this->view->item = $obj->fetchRow('id='.$id);
                    $this->view->item->id = $id;
                    
                    <?php foreach($fields as $f => $t) { ?>if( $params['<?php echo $f;?>']) $this->view->item-><?php echo $f;?> = $params['<?php echo $f;?>'];
                    <?php } ?>                
                }
            }
        } else {
            $id = (int)$this->_request->getParam('id', 0);
            if ($id > 0) {
                $this->view->item = $obj->fetchRow('id='.$id);
            }
        }

        // additional view fields required by form
        $this->view->action = 'edit';
        
        $this->render();
    }
    
    function deleteAction()
    {
        $Rights = new Rights();
        if(!$Rights->CanDo($this->view->user->id,'<?php echo $table;?>','delete')) $this->_redirect(DEFAUL_URL);
        
        $this->view->content_title = $this->view->title = "Delete <?php echo ucfirst($table);?>";
        
        $obj = new <?php echo ucfirst($table);?>();
        if (strtolower($_SERVER['REQUEST_METHOD']) == 'post') {
            Zend_Loader::loadClass('Zend_Filter_Alpha');
            $filter = new Zend_Filter_Alpha();
            
            $id = (int)$this->_request->getPost('id');
            $del = $filter->filter($this->_request->getPost('del'));
            
            if ($del == 'Yes' && $id > 0) {
                $data = array(
                        'deleted' => '1'
                );
                $where = 'id = ' . $id;
                $obj->update($data, $where);
            }
        } else {
            $id = (int)$this->_request->getParam('id');
            if ($id > 0) {
                // only render if we have an id and can find the <?php echo $table;?>.
                $this->view->item = $obj->fetchRow('id='.$id);
                
                if ($this->view->item->id > 0) {
                    $this->render();
                    return;
                }
            }
        }
        
        $this->_redirect('/<?php echo $table;?>');
    }
    
}